package com.gxd.business.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxd.business.domain.CallTrack;
import com.gxd.business.enums.DeletedEnum;
import com.gxd.business.mapper.CallTrackMapper;
import com.gxd.business.service.CallTrackService;
import com.gxd.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author gxd
 * @since 2025/6/17
 */
@Service
public class CallTrackServiceImpl extends ServiceImpl<CallTrackMapper, CallTrack> implements CallTrackService {

    @Autowired
    private CallTrackMapper callTrackMapper;

    /**
     * 添加或修改-通话记录
     * @param callTrack 通话记录实体
     * @return 通话记录
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public CallTrack addOrEdit(CallTrack callTrack) {
        // 校验参数
        Assert.notNull(callTrack, "通话记录不能为空");
        // 判断通话记录id是否为空
        if (StrUtil.isNotBlank(callTrack.getId())) {
            // 不为空，修改
            CallTrack callTrackInfo = Assert.notNull(this.getById(callTrack.getId()), "要修改的通话记录不存在");
            // 拷贝
            BeanUtils.copyNotEmptyProperties(callTrack, callTrackInfo);
            // 更新
            this.updateById(callTrackInfo);
            return callTrackInfo;
        }
        // 赋值
        callTrack.setGUID();
        callTrack.setDeleted(DeletedEnum.NO.getCode());
        // 新增
        this.save(callTrack);
        return callTrack;
    }
}
